package com.bean;

public class Sort {
    public static int a[] = new int[]{5, 9, 11, 2, 4, 74, 69, 23, 46, 1};

    public static void quickSort(int low, int high) {

        int i = low;
        int j = high;

        int pivot = low + (high - low) / 2;
        //取基准值为数组中段
        while (i <= j) {
            while (a[low] < a[pivot]) {
                low++;
            }
            while (a[high] > a[pivot]) {
                high--;
            }
            if (i <= j) {
                int temp = a[i];
                a[i] = a[j];
                a[j] = temp;
                i++;
                j--;
            }
        }
        if (low < j) {
            quickSort(low, j);
        }
        if (i < high) {
            quickSort(i, high);
        }
    }


    public static void insertSort(int a[]) {
        for (int i = 1; i < a.length; i++) {
            for (int j = i; j > 0; j--) {
                if (a[j - 1] < a[j]) {
                    int temp = a[j - 1];
                    a[j - 1] = a[j];
                    a[j] = temp;
                } else {
                    break;
                }
            }
        }
    }

    public static void bubbleSort(int a[]) {
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = i + 1; j < a.length; j++) {
                if (a[j] < a[i]) {
                    int temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
    }

    public static void selectSort(int a[]) {
        int index, temp;
        for (int i = 0; i < a.length - 1; i++) {
            index = i;
            for (int j = i + 1; j < a.length; j++) {
                if (a[j] < a[index]) {
                    index = j;
                }
            }
            if (index != i) {
                temp = a[index];
                a[index] = a[i];
                a[i] = temp;
            }
        }
    }

  /*  public static void insertSort(int a[]){
        for (int i = 1; i < a.length; i++) {
            for (int j = i; j >0 ; j--) {
                if (a[j]<a[j-1]){
                    int temp=a[j];
                    a[j]=a[j-1];
                    a[j-1]=temp;
                }else {
                    break;
                }
            }
        }
    }


    public static void selectSort(int a[]){
        int index,temp;
        for (int i = 0; i <a.length-1 ; i++) {
            index=i;
            for (int j = i+1; j < a.length ; j++) {
                if(a[index]>a[j])
                index=j;
            }
            if (index!=i){
                temp=a[i];
                a[i]=a[index];
                a[index]=temp;
            }
        }
    }

    public static void bubbleSort(int a[]) {

        for (int i = 0; i < a.length - 1; i++) {

            for (int j = i + 1; j < a.length; j++) {
                if (a[i] > a[j]) {
                    int temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
    }*/

    public static void main(String[] args) {


        quickSort(0, a.length - 1);
        System.out.println("  ");
        System.out.println("快速排序后:");
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }




        /*        int arrays[]=new int[]{5,9,11,2,4,74,69,23,46,1};
         *//*        selectSort(arrays);
        System.out.println("选择排序后:");
        for (int i = 0; i < arrays.length; i++) {
            System.out.print(arrays[i]+" ");
        }
        System.out.println("  ");
        System.out.println("冒泡排序后:");
        bubbleSort(arrays);
        for (int i = 0; i < arrays.length; i++) {
            System.out.print(arrays[i]+" ");
        }*//*
        System.out.println("  ");
        System.out.println("插入排序后:");
        insertSort(arrays);
        for (int i = 0; i < arrays.length; i++) {
            System.out.print(arrays[i]+" ");
        }*/


    }
}
